package com.igorkundovic.guitarstab.pages;

import org.apache.tapestry5.annotations.ApplicationState;
import org.apache.tapestry5.annotations.CleanupRender;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.igorkundovic.guitarstab.User;
import com.igorkundovic.guitarstab.beans.UserBean;
import com.igorkundovic.guitarstab.service.UserService;




public class Register {
	
	@Persist("flash")
	@Property
	private User user;
	
	@Property
	@Persist
	private String message;
	
	@Inject
	private UserService userService;
	
	@SetupRender
	public void setUp(){
		
		user = new UserBean();
	}
	
	

	@OnEvent(component="userform")
	public Object register(){
		message =null;
		System.out.println(user.getUserName());
		User registeredUser = userService.retrive(user);
		if(registeredUser == null){
			Long id = userService.save(user);
			System.out.println(userService.get(id).getUserName());
			return Welcome.class;
		}
		
		return this;
	}
	@CleanupRender
	public void cleanUp(){
		message =null;
	}
}
